extends Control

onready var font = preload("res://resource/font.tres")
onready var Note = preload("res://object/Note.tscn")
var bg_line_width = 400
var bg_line_height = 20
var grid = "all"
var draw_midi_data = []
func _ready():
	pass
	
	
func _draw():
	var key_width = 50
	var key_height = 20
	var y_index = [11,10,9,8,7,6,5,4,3,2,1,0]
	
	# 从11往回数，对应123456789101112个音
	for y in y_index:
		match y:
			0:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.white,true)
			1:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.black,true)
			2:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.white,true)
			3:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.black,true)
			4:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.white,true)
			5:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.black,true)
			6:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.white,true)
			7:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.white,true)
				draw_line(Vector2(0,y*key_height),Vector2(key_width,y*key_height),Color.black,1.0)
			8:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.black,true)
			9:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.white,true)
			10:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.black,true)
			11:
				draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.white,true)
				draw_string(font,Vector2(key_width-20,y*key_height+key_height),"C4",Color.black)
	
	# 绘制背景 感觉背景绘制好像有问题，按照时值分割时候好像位置不对
	for y in y_index:
		match y:
			11:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.white)
			10:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.gray)
			9:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.white)
			8:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.gray)
			7:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.white)
			6:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.white)
			5:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.gray)
			4:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.white)
			3:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.gray)
			2:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.white)
			1:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.gray)
			0:
				draw_rect(Rect2(key_width,y*key_height,bg_line_width,bg_line_height),Color.white)
	#draw_line(Vector2(key_width,11*key_height),Vector2(key_width,0),Color.black)
	match grid:
		"twofen":
			draw_line(Vector2(bg_line_width/2,0),Vector2(bg_line_width/2,12*bg_line_height),Color.black)
		"fourfen":
			draw_line(Vector2(bg_line_width/4*1,0),Vector2(bg_line_width/4*1,12*bg_line_height),Color.black)
			draw_line(Vector2(bg_line_width/4*2,0),Vector2(bg_line_width/4*2,12*bg_line_height),Color.black)
			draw_line(Vector2(bg_line_width/4*3,0),Vector2(bg_line_width/4*3,12*bg_line_height),Color.black)
			draw_line(Vector2(bg_line_width/4*4,0),Vector2(bg_line_width/4*4,12*bg_line_height),Color.black)
		"eightfen":
			draw_line(Vector2(bg_line_width/8*1,0),Vector2(bg_line_width/8*1,12*bg_line_height),Color.black)
			draw_line(Vector2(bg_line_width/8*2,0),Vector2(bg_line_width/8*2,12*bg_line_height),Color.black)
			draw_line(Vector2(bg_line_width/8*3,0),Vector2(bg_line_width/8*3,12*bg_line_height),Color.black)
			draw_line(Vector2(bg_line_width/8*4,0),Vector2(bg_line_width/8*4,12*bg_line_height),Color.black)
			draw_line(Vector2(bg_line_width/8*5,0),Vector2(bg_line_width/8*5,12*bg_line_height),Color.black)
			draw_line(Vector2(bg_line_width/8*6,0),Vector2(bg_line_width/8*6,12*bg_line_height),Color.black)
			draw_line(Vector2(bg_line_width/8*7,0),Vector2(bg_line_width/8*7,12*bg_line_height),Color.black)
			draw_line(Vector2(bg_line_width/8*8,0),Vector2(bg_line_width/8*8,12*bg_line_height),Color.black)
		"sixteenfen":
			for a in 16:
				if a == 0:
					draw_line(Vector2(bg_line_width/16*1,0),Vector2(bg_line_width/16*1,12*bg_line_height),Color.black)
				else:
					draw_line(Vector2(bg_line_width/16*a,0),Vector2(bg_line_width/16*a,12*bg_line_height),Color.black)
			pass
#	if draw_midi_data:
#		if grid == "all":
#			draw_rect(draw_midi_data[0],draw_midi_data[1],draw_midi_data[2])
#		elif grid == "twofen":
#			draw_rect(draw_midi_data[0],draw_midi_data[1]/2,draw_midi_data[2])
#		elif grid == "fourfen":
#			draw_rect(draw_midi_data[0],draw_midi_data[1]/4,draw_midi_data[2])
#		elif grid == "eightfen":
#			draw_rect(draw_midi_data[0],draw_midi_data[1]/8,draw_midi_data[2])
#		elif grid == "sixteenfen":
#			draw_rect(draw_midi_data[0],draw_midi_data[1]/16,draw_midi_data[2])
	pass
	
func _on_grid(toolbar,v):
	toolbar.hide()
	grid = v
	update()
	pass
func draw_black(y):
	#draw_rect(Rect2(0,y*key_height,key_width,key_height),Color.black,true)
	pass


func _on_PianoRoll_gui_input(event):
	if event is InputEventMouseButton and event.is_pressed():
		if event.button_index == BUTTON_LEFT:
			var note = Note.instance()
			match grid:
				"all":
					note.size = Rect2(event.position,Vector2(bg_line_width,bg_line_height))
				"twofen":
					note.size = Rect2(event.position,Vector2(bg_line_width/2,bg_line_height))
				"fourfen":
					note.size = Rect2(event.position,Vector2(bg_line_width/4,bg_line_height))
				"eightfen":
					note.size = Rect2(event.position,Vector2(bg_line_width/8,bg_line_height))
				"sixteenfen":
					note.size = Rect2(event.position,Vector2(bg_line_width/16,bg_line_height))
			add_child(note)
			note.rect_position = get_global_mouse_position()
#			draw_midi_data.clear()
#			draw_midi_data.append_array([Rect2(event.position,Vector2(bg_line_width,bg_line_height)),Color.black,true])
#			print_debug(draw_midi_data)
#			update()
#			update()
	pass # Replace with function body.
